﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using dbPoll_Application.ViewModels;
using dbPoll_Application.Models;
using System.Web.Routing;
using System.Globalization;

namespace dbPoll_Application.Controllers
{
    public class QueryController : Controller
    {
        [Authorize]
        public ActionResult Index()
        {
            var user = UserRepository.GetUserByUsername(User.Identity.Name);
            var creators = UserRepository.GetPollCreators(user.ClientCodes.FirstOrDefault<ClientCode>());
            DateTime startTime = DateTime.Now.AddDays(-1);
            DateTime endTime = DateTime.Now;

            return View(new QueryViewModel(startTime, endTime, creators));
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(QueryViewModel q)
        {
            var user = UserRepository.GetUserByUsername(User.Identity.Name);
            var questions = QuestionRepository.GetQuestionsByTimeCreatedAndPollCreator(q.startTime, q.endTime, q.selectedPollCreator.UserName);
            var creators = UserRepository.GetPollCreators(user.ClientCodes.FirstOrDefault<ClientCode>());

            return View(new QueryViewModel(q.startTime, q.endTime, questions, creators));
        }
    }
}
